package com.atguigu.daijia.driver.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.DriverStatusEnum;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.dispatch.client.NewOrderFeignClient;
import com.atguigu.daijia.driver.client.DriverInfoFeignClient;
import com.atguigu.daijia.driver.service.DriverService;
import com.atguigu.daijia.map.client.LocationFeignClient;
import com.atguigu.daijia.model.form.driver.DriverFaceModelForm;
import com.atguigu.daijia.model.form.driver.UpdateDriverAuthInfoForm;
import com.atguigu.daijia.model.vo.driver.DriverAuthInfoVo;
import com.atguigu.daijia.model.vo.driver.DriverLoginVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service("driverService")
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverServiceImpl implements DriverService {
    @Resource
    NewOrderFeignClient newOrderFeignClient;

    @Resource
    LocationFeignClient locationFeignClient;

    @Resource
    private DriverInfoFeignClient driverInfoClient;

    @Resource
    private RedisTemplate redisTemplate;


    /*
     * @param code 
     * @==============History===============<br/>;
     * @Description //   小程序授权登录
     * @Date 2024/8/1
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    @Override
    public String login(String code) {
        //远程调用，得到司机id
        Result<Long> loginResult = driverInfoClient.login(code);
        Integer codeResult = loginResult.getCode();
        if(codeResult != 200){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        Long driverId = loginResult.getData();
        //token字符串
        String token = UUID.randomUUID().toString().replaceAll("-","");

        //放到redis，设置过期时间
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX + token,
                driverId.toString(),
                RedisConstant.USER_LOGIN_KEY_TIMEOUT,
                TimeUnit.SECONDS);

        return token;
    }

    /*
     * @param driverId
     * @==============History===============<br/>;
     * @Description //   获取司机的认证信息
     * @Date 2024/8/7
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    @Override
    public DriverAuthInfoVo getDriverAuthInfo(Long driverId) {
        Result<DriverAuthInfoVo> authInfoVoResult = driverInfoClient.getDriverAuthInfo(driverId);
        return authInfoVoResult.getData();
    }

    /*
     * @param updateDriverAuthInfoForm
     * @==============History===============<br/>;
     * @Description //   更新司机的认证信息
     * @Date 2024/8/8
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    @Override
    public Boolean updateDriverAuthInfo(UpdateDriverAuthInfoForm updateDriverAuthInfoForm) {
        Result<Boolean> booleanResult = driverInfoClient.updateDriverAuthInfo(updateDriverAuthInfoForm);
        return booleanResult.getData();
    }

    /*
     * @param driverFaceModelForm 
     * @==============History===============<br/>;
     * @Description //   创建司机人脸模型
     * @Date 2024/8/8
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    @Override
    public Boolean creatDriverFaceModel(DriverFaceModelForm driverFaceModelForm) {
        Result<Boolean> booleanResult = driverInfoClient.creatDriverFaceModel(driverFaceModelForm);
        return booleanResult.getData();
    }

    /*
     * @param driverId 
     * @==============History===============<br/>;
     * @Description //   判断司机当日是否进行过人脸识别
     * @Date 2024/8/8
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    @Override
    public Boolean isFaceRecognition(Long driverId) {
        return driverInfoClient.isFaceRecognition(driverId).getData();
    }

    /*
     * @param driverFaceModelForm 
     * @==============History===============<br/>;
     * @Description //   验证司机人脸
     * @Date 2024/8/8
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    @Override
    public Boolean verifyDriverFace(DriverFaceModelForm driverFaceModelForm) {
        return driverInfoClient.verifyDriverFace(driverFaceModelForm).getData();
    }

    /*
     * @param driverId   司机id
     * @==============History===============<br/>
     * @Description //   司机开始接单
     * @Date 2024-8-26 21:01
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return java.lang.Boolean
     */
    @Override
    public Boolean startService(Long driverId)
    {
        // 1.判断当前司机认证状态;0未认证,1审核中,2已认证
        DriverLoginVo driverLoginVo = driverInfoClient.getDriverLoginInfo(driverId).getData();
        Integer authStatus = driverLoginVo.getAuthStatus();

        if(authStatus != 2)
        {
            throw new GuiguException(ResultCodeEnum.AUTH_ERROR);
        }

        // 2.判断当日是否人脸识别
        Boolean isFaceFlag = driverInfoClient.isFaceRecognition(driverId).getData();
        if(!isFaceFlag)
        {
            throw new GuiguException(ResultCodeEnum.FACE_ERROR);
        }
        // 3.更新司机接单状态;1:开始接单 0:未接单
        driverInfoClient.updateServiceStatus(driverId, DriverStatusEnum.START_SERVER_STATUS.getCode());

        // 4.删除司机位置信息
        locationFeignClient.removeDriverLocation(driverId);

        // 5.清空司机订单队列
        newOrderFeignClient.clearNewOrderQueueData(driverId);

        return true;
    }

    @Override
    public Boolean stopService(Long driverId) {

        // 1.更新司机接单状态;1:开始接单 0:未接单
        driverInfoClient.updateServiceStatus(driverId, DriverStatusEnum.END_SERVER_STATUS.getCode());

        // 2.删除司机位置信息
        locationFeignClient.removeDriverLocation(driverId);

        // 3.清空司机订单队列
        newOrderFeignClient.clearNewOrderQueueData(driverId);

        return true;
    }
}
